<?php
namespace XtwsAdmin\Controller;
use Think\Controller;

class NewsController extends CommonController{

    // 文章列表
    public function index(){
        $news = M('news');
        $cate = M('cate');
        $where = "";
        if ($_GET['keyword'] != '') {
            $where = "title like '%" . $_GET['keyword'] . "%'";
            $this->assign('keyword', $_GET['keyword']);// 赋值分页输出
        }
        $count = $news->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('header',"");
        $Page->lastSuffix=false;
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();// 分页显示输出
        $news_arr = $news->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        foreach ($news_arr as $key=>$val){//循环文章
            $news_arr[$key]['cate_name'] = $cate->where("id=".$val['pid'])->getField('title');

        }// end if
//        $map1['keywords'] = array('neq','');
//        $cate_list = $cate->where($map1)->field('keywords,id')->select();
//        $news_arr1 = $news->where($where)->order('id desc')->limit(4000,6000)->select();
//        foreach ($news_arr1 as $key=>$val){//循环文章
//            if($val['pid']==0){
//                foreach ($cate_list as $k=>$v){//循环分类数组
//                    $cate_keywords = explode(',',$v['keywords']);
//                    foreach ($cate_keywords as $va){
//                        $state1 = strstr($val['title'],$va);    //比较分类与该关键词
//                        if ($state1!=false){//如果标题包含该关键词，则文章绑定该关键词对应的分类
//                            $news->where('id='.$val['id'])->setField('pid',$v['id']);
//                            goto next_ARC;
//                        }
//                    }
//                }
//                next_ARC:
//            }
//            if($val['addtime']==0){
//                $news->where('id='.$val['id'])->setField('addtime',time());
//            }
//        }// end if
        $this->assign('list', $news_arr);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }// end fun


    //  添加文章
    public function addNews(){
        $news = M('news');
        $cate = M('cate');
        if (IS_POST) {
            $data = $_POST;
            $data['addtime'] = time();
            $res = $news->add($data);
            if ($res) {
                $this->success('提交成功', '/XtwsAdmin/News/');
            } else {
                $this->error('提交失败');
            }// end if
        } else {
            //查询所有分类
//            dump(1);
            $cate_list = $cate->field('id,pid,title')->select();
            $cate_list = list_to_tree($cate_list); //转换为分类树
            $this -> assign('cate_list',$cate_list);
            $this->display();
        }// end if
    }//end fun add

    //  修改文章
    public function editNews(){
        $news = M('news');
        $cate = M('cate');
        $id = intval(I('id'));
        if (IS_POST) {
            $data = $_POST;
//            dump($data);die;
            $res = $news->save($data);
            if ($res) {
                $this->success('提交成功', '/XtwsAdmin/News/');
            } else {
                $this->error('提交失败');
            }// end if
        } else {
            //查询所有分类
//            dump(1);
            $info = $news->where('id='.$id)->find();
            $cate_list = $cate->field('id,pid,title')->select();
            $cate_list = list_to_tree($cate_list); //转换为分类树
            $this -> assign('cate_list',$cate_list);
            $this -> assign('info',$info);
            $this->display();
        }// end if
    }//end fun add

    // 删除文章
    public function del(){
        $id = intval(I('id'));
        if ($id) {
            $news = M('news');
            $res = $news->delete($id);
            if ($res) {
                $this->success('删除成功');
            } else {
                $this->error('出错啦，请检查');
            }
        } else {
            $this->error('请先选择文章');
        }//end fun

    }


}// end class